package com.foreknow.view;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddGuestbook extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddGuestbook frame = new AddGuestbook();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddGuestbook() {
		setTitle("add guestbook");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblName = new JLabel("name:");
		lblName.setBounds(70, 25, 54, 15);
		contentPane.add(lblName);

		textField = new JTextField();
		textField.setBounds(120, 22, 183, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		JLabel lblPhone = new JLabel("phone:");
		lblPhone.setBounds(70, 60, 54, 15);
		contentPane.add(lblPhone);

		textField_1 = new JTextField();
		textField_1.setBounds(120, 57, 183, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblEmail = new JLabel("email:");
		lblEmail.setBounds(70, 101, 54, 15);
		contentPane.add(lblEmail);

		textField_2 = new JTextField();
		textField_2.setBounds(120, 98, 183, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		JLabel lblTitle = new JLabel("title:");
		lblTitle.setBounds(70, 139, 54, 15);
		contentPane.add(lblTitle);

		textField_3 = new JTextField();
		textField_3.setBounds(120, 129, 183, 21);
		contentPane.add(textField_3);
		textField_3.setColumns(10);

		JLabel lblContent = new JLabel("content:");
		lblContent.setBounds(70, 164, 54, 15);
		contentPane.add(lblContent);

		textField_4 = new JTextField();
		textField_4.setBounds(120, 164, 183, 21);
		contentPane.add(textField_4);
		textField_4.setColumns(10);

		JButton btnNewButton = new JButton("confirm");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//confirm
				//调用service层的方法save()
				GuestbookService service = new GuestbookServiceImpl();
				Guestbook gb = new Guestbook();
				gb.setName(textField.getText());
				gb.setPhone(textField_1.getText());
				gb.setEmail(textField_2.getText());
				gb.setTitle(textField_3.getText());
				gb.setContent(textField_4.getText());
				Date date = new Date();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				gb.setCreatedtime(simpleDateFormat.format(date));
				boolean isRight = service.save(gb);
				if(isRight) {
					new SwingDemo();
				}else {
					System.out.println("insert error......");
				}
			}
		});
		btnNewButton.setBounds(109, 214, 93, 23);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("cancel");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//cancel
			}
		});
		btnNewButton_1.setBounds(244, 214, 93, 23);
		contentPane.add(btnNewButton_1);
		// 显示窗口
		setVisible(true);
	}
}
